package com.uxwine.alarm.main;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAlarms extends SQLiteOpenHelper {

	protected static final String DB_NAME 			= "alarms.db";
	protected static final String DB_TABLE 			= "tb_alarm";
	protected static final int DB_VERSION 			= 1;
	
	protected static final String KEY_ID 			= "_id";
	protected static final String KEY_NAME 			= "_name";
	protected static final String KEY_TIME 			= "_time";
	protected static final String KEY_EFFECTIVE		= "_effective";
	protected static final String KEY_CYCLE 		= "_cycle";
	
	protected SQLiteDatabase db = null;
	
	public DBAlarms(Context context, boolean bWriteable)
	{
		super(context, DB_NAME, null, DB_VERSION);
		db = bWriteable ? getWritableDatabase() : getReadableDatabase();
	}
	
	public DBAlarms(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		db = getWritableDatabase();
	}
	
	@Override
	public void onCreate(SQLiteDatabase sql) {
		sql.execSQL("CREATE TABLE IF NOT EXISTS " + DB_TABLE + "(" + KEY_ID + 
				" INTEGER PRIMARY KEY AUTOINCREMENT ," + KEY_NAME + " TEXT, " + 
				KEY_TIME + " INTEGER, "+ KEY_CYCLE +" INTEGER, " + KEY_EFFECTIVE+" INTEGER)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase sql, int oldVersion, int newVersion) {
		sql.execSQL("DROP TABLE IF EXISTS" + DB_TABLE);
		this.onCreate(sql);
	}
	
	public void close()
	{
		if(null != db){
			db.close();
			db = null;
		}
	}
	
	public void cleanData()
	{
		db.delete(DB_TABLE, KEY_ID + "> 0", null);
	}
	
	public void put(Alarm entry)
	{
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, entry.getName());
		values.put(KEY_TIME, entry.getTime());
		values.put(KEY_CYCLE, entry.getCycle());
		values.put(KEY_EFFECTIVE, entry.isEffective() ? 1 : 0);
		entry.setId((int)db.insert(DB_TABLE, null, values));
	}
	
	public void update(Alarm entry)
	{		
		ContentValues values = new ContentValues();
		values.put(KEY_ID, entry.getId());
		values.put(KEY_NAME, entry.getName());
		values.put(KEY_TIME, entry.getTime());
		values.put(KEY_CYCLE, entry.getCycle());
		values.put(KEY_EFFECTIVE, entry.isEffective() ? 1 : 0);
		
		db.update(DB_TABLE, values, KEY_ID + "=?", new String[]{entry.getId() + ""});
	}	
	
	public List<Alarm> getAll()
	{
		List<Alarm> list = new ArrayList<Alarm>();
		Cursor c = db.query(DB_TABLE, null, null, null, null, null, KEY_EFFECTIVE+" desc");
		if(null == c)return list;
		
		int idxId = c.getColumnIndex(KEY_ID);
		int idxName = c.getColumnIndex(KEY_NAME);
		int idxTime = c.getColumnIndex(KEY_TIME);
		int idxCycle = c.getColumnIndex(KEY_CYCLE);
		int idxEffect = c.getColumnIndex(KEY_EFFECTIVE);
		
		while(c.moveToNext())
		{
			Alarm a = new Alarm();
			a.setTime(c.getLong(idxTime));
			a.setId(c.getInt(idxId));
			a.setName(c.getString(idxName));
			a.setCycle(c.getInt(idxCycle));
			a.setEffective(c.getInt(idxEffect) == 0 ? false : true);
			list.add(a);
		}
		c.close();		
		return list;
	}
	
	public Alarm get(int id)
	{
		Alarm a = null;
		Cursor c = db.query(DB_TABLE, null, KEY_ID + "=" + id, null, null, null, null);
		if(null == c)return a;
		
		int idxId = c.getColumnIndex(KEY_ID);
		int idxName = c.getColumnIndex(KEY_NAME);
		int idxTime = c.getColumnIndex(KEY_TIME);
		int idxCycle = c.getColumnIndex(KEY_CYCLE);
		int idxEffect = c.getColumnIndex(KEY_EFFECTIVE);
		if(c.getCount() == 1 && c.moveToNext()){
			a = new Alarm();
			a.setTime(c.getLong(idxTime));
			a.setId(c.getInt(idxId));
			a.setName(c.getString(idxName));
			a.setCycle(c.getInt(idxCycle));
			a.setEffective(c.getInt(idxEffect) == 0 ? false : true);					
		}
		c.close();
		return a;
	}
	
	public void delete(int id){
		db.delete(DB_TABLE, KEY_ID + "=?", new String[]{id+""});
	}
}
